//
//  colorPickerView.m
//  TextNote
//
//  Created by rimi on 16/5/14.
//  Copyright © 2016年 BlueSky. All rights reserved.
//

#import "BSKColorPickerView.h"

UIView * views[12];

@interface BSKColorPickerView ()
@property(nonatomic,assign)CGPoint firstPoint;
@end

@implementation BSKColorPickerView

- (instancetype)initWithFrame:(CGRect)frame
{
	self = [super initWithFrame:frame];
	if (self) {
		self.hidden = YES;
		self.backgroundColor = [UIColor clearColor];
		[self setColors];
		_column = 4;
	}
	return self;
}

//默认可供选择的颜色
-(void)setColors{
	_colors = @[
				[UIColor colorWithRed:0.9697 green:0.3054 blue:0.3896 alpha:1.0],
				[UIColor colorWithRed:0.99 green:0.5873 blue:0.326 alpha:1.0],
				[UIColor colorWithRed:0.9975 green:0.9239 blue:0.3678 alpha:1.0],
				[UIColor colorWithRed:0.5193 green:1.0 blue:0.5041 alpha:1.0],
				[UIColor colorWithRed:0.3625 green:0.5261 blue:0.9983 alpha:1.0],
				[UIColor colorWithRed:0.3704 green:0.999 blue:0.9965 alpha:1.0],
				[UIColor colorWithRed:0.8876 green:0.3652 blue:0.9987 alpha:1.0],
				[UIColor colorWithRed:0.502 green:0.3112 blue:0.0 alpha:1.0],
				[UIColor colorWithRed:0.9974 green:0.5865 blue:0.7606 alpha:1.0],
				[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0],
				[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0],
				[UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1.0]
				];
}
//显示选择器
-(void)show{
	_firstPoint = self.center;
	_firstPoint.x = _firstPoint.x - ((50*_column) -50)/2;
	_firstPoint.y =_firstPoint.y - (50*(_colors.count/_column) - 50)/2;
	
	self.hidden = NO;
	
	[UIView animateWithDuration:0.3 animations:^{
		self.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.4];
	}];
	for (NSInteger index = 0; index<_colors.count; index++) {
		UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 2, 2)];
		view.backgroundColor =_colors[index];
		view.center = CGPointMake(_firstPoint.x+(index%_column)*50, _firstPoint.y+(index/_column)*50);
		view.tag = 1001+index;
		view.layer.cornerRadius = 1;
		view.layer.shadowColor = [[UIColor whiteColor] CGColor];
		view.layer.shadowOffset = CGSizeMake(0, 0);
		view.layer.shadowRadius = 0.15;
		view.layer.shadowOpacity = 1;
		view.alpha = 0;
		views[index] = view;
		[self addSubview:view];
		
		[UIView animateWithDuration:0.5 delay:0.05*index usingSpringWithDamping:0.5 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
			view.transform = CGAffineTransformMakeScale(20, 20);
			view.alpha = 1;
		} completion:^(BOOL finished) {
		}];
		[view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(PickerAction:)]];
	}
	
	[self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(PickerAction:)]];

}

-(void)PickerAction:(UITapGestureRecognizer *)gesture{
	
	if (gesture.view.tag-1001 < _colors.count) {
		if (_onColorSelected!=nil) {
			_onColorSelected(_colors[gesture.view.tag-1000-1]);
		}
		[self hideself];
	}else {
		if (_onColorSelected!=nil) {
			_onColorSelected(nil);
		}
		[self hideself];
	}
}

//隐藏颜色选择器并将自己从父视图中移除
-(void)hideself{
	self.userInteractionEnabled = NO;
	[UIView animateWithDuration:0.3 animations:^{
		self.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
	}];
	for (NSInteger i = 0; i<_colors.count; i++) {
		UIView * v =views[i];
		[UIView animateWithDuration:0.5 delay:0.025*i usingSpringWithDamping:0.5 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
			v.transform = CGAffineTransformMakeScale(1, 1);
			v.alpha = 0;
		} completion:^(BOOL finished) {
			if (i == _colors.count-1) {
				self.hidden = YES;
				[self removeFromSuperview];
			}
		}];
	}
}
@end
